import {
    GetStoreSmsConfig,
    GetSmsSendRecordListWithPage,
    GetSmsRechargeRecordListWithPage,
    GetSmsSignName,
    SendTestMsg,
    ChangeSignName,
    GetOrderProfileData
} from '@/services/store/stortMessage';
import {catchException} from '@/utils/utils';
export default {
    namespace: 'stortMessage',

    state: {
        storeSmsData: [],
        storeSmsList: [],
        recordList:[],
        signName: [],
        orderProfileData: {}
    },

    effects: {

        // 获取店铺短信信息
        *GetStoreSmsConfig({payload, callBack}, {call, put}) {
            const data = yield call(GetStoreSmsConfig, payload);
            yield catchException(function* (){
              if(data.Code === 0) {
                yield put({
                    type: 'saveGetStoreSmsConfig',
                    payload: {
                        dataList: data.Data,
                    },
                });
                callBack && callBack.success(data);
              } else {
                  callBack && callBack.error(data)
              }
            })

        },

        // 获取店铺短信发送记录(分页)
        *GetSmsSendRecordListWithPage({payload, callBack}, {call, put}) {
          const data = yield call(GetSmsSendRecordListWithPage, payload);
          yield catchException(function* (){
            if(data.Code === 0) {
              yield put({
                  type: 'saveGetSmsSendRecordListWithPage',
                  payload: {
                      dataList: data.Data,
                  },
              });
              callBack && callBack.success(data);
            } else {
                callBack && callBack.error(data)
            }
          })

      },

        // 获取店铺短信充值记录(分页)
        *GetSmsRechargeRecordListWithPage({payload, callBack}, {call, put}) {
              const data = yield call(GetSmsRechargeRecordListWithPage, payload);
              yield catchException(function* (){
                if(data.Code === 0) {
                  yield put({
                      type: 'saveGetSmsRechargeRecordListWithPage',
                      payload: {
                          dataList: data.Data,
                      },
                  });
                  callBack && callBack.success(data);
                } else {
                    callBack && callBack.error(data)
                }
              })

        },

        // 获取店铺短信签名
         *GetSmsSignName({payload, callBack}, {call, put}) {
                const data = yield call(GetSmsSignName, payload);
                yield catchException(function* (){
                  if(data.Code === 0) {
                    yield put({
                        type: 'saveGetSmsSignName',
                        payload: {
                            dataList: data.Data,
                        },
                    });
                    callBack && callBack.success(data);
                  } else {
                      callBack && callBack.error(data)
                  }
                })

        },

             // 概况
             *GetOrderProfileData({payload, callBack}, {call, put}) {
                const data = yield call(GetOrderProfileData, payload);
                yield catchException(function* (){
                  if(data.Code === 0) {
                    yield put({
                        type: 'saveGetOrderProfileData',
                        payload: {
                            dataList: data.Data,
                        },
                    });
                    callBack && callBack.success(data);
                  } else {
                      callBack && callBack.error(data)
                  }
                })

        },

        // 修改签名发送测试短信
        *SendTestMsg({payload, callBack}, {call, put}) {
            const data = yield call(SendTestMsg, payload);
            yield catchException(function* (){
              if(data.Code === 0) {
                callBack && callBack.success(data);
              } else {
                  callBack && callBack.error(data)
              }
            })

        },

        // 修改短信签名
        *ChangeSignName({payload, callBack}, {call, put}) {
            const data = yield call(ChangeSignName, payload);
            yield catchException(function* (){
              if(data.Code === 0) {
                callBack && callBack.success(data);
              } else {
                  callBack && callBack.error(data)
              }
            })

        },


    },

    reducers: {
        saveGetStoreSmsConfig(state, {payload}) {
            return {
                ...state,
                storeSmsData: payload.dataList,
            };
        },
        saveGetSmsSendRecordListWithPage(state, {payload}) {
          return {
              ...state,
              storeSmsList: payload.dataList,
          };
      },
      saveGetSmsRechargeRecordListWithPage(state, {payload}) {
        return {
            ...state,
            recordList: payload.dataList,
        };
    },
    saveGetSmsSignName(state, {payload}) {
        return {
            ...state,
            signName: payload.dataList,
        };
    },
    saveGetOrderProfileData(state, {payload}) {
        return {
            ...state,
            orderProfileData: payload.dataList,
        };
    },
    }
}
